package ai.plantdata.dao.strategy.impl;

import ai.plantdata.dao.dto.UnrelatedConceptSeparateTab;
import ai.plantdata.dao.strategy.SeparateHandle;
import ai.plantdata.dao.util.JdbcUtils;
import ai.plantdata.graph.api.schema.ConceptDefinition;
import ai.plantdata.graph.mongo.MongoGraphStore;
import ai.plantdata.vo.DataSourceConfig;
import ai.plantdata.dao.util.BeanTransform;
import ai.plantdata.vo.SeparateTable;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @Author: WangHui
 * @Date: 2021/7/16 17:19
 * @Description:
 */
public class NoAssociationConcept implements SeparateHandle {
    /**
     * 独立无关联表中读取概念
     *
     * @param separateDTO
     * @param dataSourceConfig
     * @return
     */
    @Override
    public List<ConceptDefinition> schemaHandle(SeparateTable separateDTO, DataSourceConfig dataSourceConfig, MongoGraphStore store) {
        UnrelatedConceptSeparateTab nameDTO = BeanTransform.separateTableToUnrelatedConceptSeparateTab(separateDTO);
        Connection conn = JdbcUtils.getConnection(dataSourceConfig);
        List<ConceptDefinition> conceptDefinitions = new ArrayList<>();
        String tableName = nameDTO.getTableName();
        String name = nameDTO.getName();
        ConceptDefinition parentConcept = null;
        if (name != null) {
            parentConcept = ConceptDefinition.create(name, nameDTO.getMeanTag());
        }
        String nameField = nameDTO.getNameField();
        String meanTagField = nameDTO.getMeanTagField();
        List<Map<String, String>> token = getToken(conn, tableName, nameField, meanTagField);
        for (Map<String, String> map : token) {
            ConceptDefinition conceptDefinition = ConceptDefinition.create(map.get("name"), map.get("meanTag"));
            if (parentConcept != null) {
                conceptDefinition.setParent(parentConcept);
            }
            conceptDefinitions.add(conceptDefinition);
        }

        return conceptDefinitions;
    }

    /**
     * 概念表中无实体
     *
     * @param separateDTO
     * @return
     */
    @Override
    public void entityHandle(SeparateTable separateDTO, DataSourceConfig dataSourceConfig, MongoGraphStore store) {
    }
}
